import { createStore, storeKey } from "vuex";
import axios from "axios";

export default createStore({
  state: {
    description: "", //活动说明
    maxList: 0, //最大队列
    scene: 48, //场景
    pinDaoList: [], //频道公司
    playList: {
      48: { data: { current: {}, list: [] } },
      49: { data: { current: {}, list: [] } },
    }, //当前队列
    isPlaying: false,
    isDemand: false,
    playIngNo: 1,
    searchList: [],
    isAdmin: false,
    audioList: [],
    userId: "9999",
    webList: [],
    demandDetails: {},
  },
  getters: {},
  setup(props, ctx) {
    const formateTime = (time) => {
      const h = parseInt(time / 3600);
      const minute = parseInt((time / 60) % 60);
      const second = Math.ceil(time % 60);

      const hours = h < 10 ? "0" + h : h;
      const formatSecond = second > 59 ? 59 : second;
      return `${hours > 0 ? `${hours}:` : ""}${
        minute < 10 ? "0" + minute : minute
      }:${formatSecond < 10 ? "0" + formatSecond : formatSecond}`;
    };
    return {
      formateTime,
    };
  },
  mutations: {
    // setBase(arr){
    //   state.pdaoList=arr.pdaoList
    // }
    setBase(state, res) {
      console.log("写入频道数据");
      let { baseData, pinDaoGrade, sceneList } = res;
      state.description =
        "喜迎党的二十大、共庆建团百年，“青春向党 百年如歌——我的青春在芒果”五四青年活动在湖南广电浓情开展，全体芒果青年共同栽下青春之树，深情讲述青春故事。";
      state.maxList = baseData.maxList;
      state.pinDaoList = pinDaoGrade;
      state.playList = sceneList;
    },
    setScene(state, val) {
      // console.log('写入场景',val)
      state.scene = val;
      sessionStorage.setItem("scene", val);
    },
    setPlayer(state, arr) {
      state.isPlaying = arr.isPlaying;
      state.playIngNo = arr.playIngNo;
    },
    setDemandDetails(state, val) {
      state.demandDetails = val;
    },
    setDemand(state, val) {
      state.isDemand = val;
    },
    setSearch(state, arr) {
      state.searchList = arr;
    },
    setAdmin(state, val) {
      state.isAdmin = val;
      sessionStorage.setItem("isAdmin", val);
    },
    setDepList(state, val) {
      state.pinDaoList = val;
    },
    setAudioList(state, val) {
      state.audioList = val;
      sessionStorage.setItem('audioList', JSON.stringify(val))
    },
    setPlayList(state, val) {
      state.playList = val;
    },
    setWebList(state, val) {
      state.webList = val;
    },
    setUserId(state, val) {
      state.userId = val;
    },
  },
  actions: {
    getWebList(store) {
      store.commit("setWebList", []);
    },
    baseData(store) {
      axios.get("/getalldata").then((response) => {
        store.commit("setBase", response.data);
        sessionStorage.setItem("baseData", JSON.stringify(response.data));
      });
    },
    dianBo(store, args) {
      axios
        .post("/dianbo", { scene: store.state.scene, id: args, user_id: 1 })
        .then((response) => {
          store.commit("setBase", response.data);
          sessionStorage.setItem("baseData", JSON.stringify(response.data));
          if (store.state.isPlaying === false) {
            store.commit("setPlayer", { isPlaying: true, playIngNo: 0 });
          }
        });
    },
    deletePlayItem(store, id) {
      axios.post("/deletePlayItem", { id: id }).then((response) => {
        store.commit("setBase", response.data);
        sessionStorage.setItem("baseData", JSON.stringify(response.data));
      });
    },
    getDeps(store, key) {
      axios.post("/mguo/dept/listDept", { content: key }).then((response) => {
        store.commit("setDepList", response.data);
      });
    },
    getResources(store, name) {
      axios.post("/mguo/file/listFile", { name: name }).then((response) => {
        let resources = response.data;
        let rsMap = {};
        if (resources.data) {
          for (let rs of resources.data) {
            if (!rs.deptName) {
              rs.deptName = "";
            }
            const fileResouseDOs = rs.fileResouseDOs;
            if (fileResouseDOs) {
              for (let file of fileResouseDOs) {
                const time = file.resourceTime;
                const h = parseInt(time / 3600);
                const minute = parseInt((time / 60) % 60);
                const second = Math.ceil(time % 60);

                const hours = h < 10 ? "0" + h : h;
                const formatSecond = second > 59 ? 59 : second;
                file.resourceTime = `${hours > 0 ? `${hours}:` : ""}${
                  minute < 10 ? "0" + minute : minute
                }:${formatSecond < 10 ? "0" + formatSecond : formatSecond}`;
              }
            }
            if (rs.companyName in rsMap) {
              rsMap[rs.companyName].push(rs);
            } else {
              rsMap[rs.companyName] = [rs];
            }
          }
        }
        store.commit("setAudioList", rsMap);
      });
    },
    getPlayList(store) {
      const formateTime = (time) => {
        const h = parseInt(time / 3600);
        const minute = parseInt((time / 60) % 60);
        const second = Math.ceil(time % 60);
        const hours = h < 10 ? "0" + h : h;
        const formatSecond = second > 59 ? 59 : second;
        return `${hours > 0 ? `${hours}:` : ""}${
          minute < 10 ? "0" + minute : minute
        }:${formatSecond < 10 ? "0" + formatSecond : formatSecond}`;
      };
      axios.post("/mguo/audio/playList", {}).then((response) => {
        const current = response.data.data.current;
        const list = response.data.data.list;
        if (current) {
          const time = current.resourceTime;
          current.resourceTime = formateTime(time);
          current.realDuration = current.duration;
          current.duration = formateTime(current.duration);
        }
        if (list) {
          for (let ls of list) {
            const time = ls.resourceTime;
            ls.resourceTime = formateTime(time);
            ls.realDuration = ls.duration;
            ls.duration = formateTime(ls.duration);
          }
        }
        store.commit("setPlayList", {
          48: response.data,
          49: { data: { current: {}, list: [] } },
        });
        if (response.data?.data.current) {
          store.commit("setPlayer", { isPlaying: true, playIngNo: 0 });
        } else {
          store.commit("setPlayer", { isPlaying: false, playIngNo: 0 });
        }
      });
    },
  },
  modules: {},
});
